﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SimplePMT.Data;
using System.Text;
using NHibernate.Expression;

namespace SimplePMT
{
    public partial class QualityAssurance : Components.BasePage
    {
        public string MemberID = string.Empty;
        public StringBuilder releaseList = new StringBuilder();

        [AjaxPro.AjaxMethod()]
        public int CreateQAReport(int ReleaseID)
        {
            Release r = Release.Find(ReleaseID);
            QAReport report = new QAReport();
            report.Passed = false;
            report.ReportDate = DateTime.Now;
            report.RevisionNumber = 0;
            report.Summary = "";
            report.Save();
            r.QAReportID = report.ID;
            r.Save();
            return report.ID;
        }

        [AjaxPro.AjaxMethod()]
        public string GetReleaseTasks(int ReleaseID, string ProjectName)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder rb = new StringBuilder();
            int taskReadyCount = 0;
            Task[] tasks = Task.FindAll(Expression.Eq("ReleaseID", ReleaseID));
            Release r = Release.Find(ReleaseID);
            rb.Append("<table width=100%>");
            rb.Append("<tr><td width=7% align=right><b>Release:</b></td><td bgcolor=\"#eeeeee\">" + r.Major + "." + r.Minor + "." + r.Build + " - " + ProjectName + " &nbsp;&nbsp;&nbsp;Cutoff: " + r.ReleaseDateCutoff.ToShortDateString() + "</td></tr>");
            rb.Append("<tr><td width=7% align=right valign=top><b>Notes:</b></td><td bgcolor=\"#eeeeee\">" + r.Notes.Replace("\r\n", "<p>") + "</td></tr>");
            rb.Append("</table><p>");
            if (tasks.Length == 0)
            {
                sb.Append("<tr><td width=12% align=right></td><td>No Tasks are currently associated with this release.</td></tr>");
            }
            foreach (Task task in tasks)
            {
                TaskContainerItem[] tcia = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                TaskContainer container = new TaskContainer();
                if (tcia.Length > 0)
                {
                    container = tcia[0].TaskContainer;
                }
                sb.Append("<tr><td width=12% align=right>");
                if (task.IsResolved)
                {
                    taskReadyCount++;
                    sb.Append("<img src=\"images/checkmark.png\">");
                }
                sb.Append("</td>");
                sb.Append("<td width=88% OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + container.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'").Replace("\"", "&quot;") + "');\" OnMouseOver=\"this.className='highlightRow';this.style.cursor='pointer';\" OnMouseOut=\"this.className='';\">" + task.Title + "</td></tr>");
                
            }
            if (r.QAReportID > 0)
            {
                sb.Append("<tr><td colspan=2 align=right><button type=\"button\" OnClick=\"openQAReport(" + r.QAReportID + ");\">Open QA Report</button></td></tr>");
            }
            else if (taskReadyCount.Equals(tasks.Length))
            {
                sb.Append("<tr><td colspan=2 align=right><button type=\"button\" OnClick=\"newQAReport(" + r.ID + ");\">New QA Report</button>&nbsp;&nbsp;&nbsp;</td></tr>");
            }
            return rb.ToString() +"<table width=100%>" + sb.ToString() + "</table>";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(QualityAssurance), this.Page);
            ValidateSession();
            MemberID = Session["MemberID"].ToString();
            TaskContainer[] projects = TaskContainer.FindAll(Expression.Eq("ParentContainerID", 0), Expression.Eq("IsAWISHProject", true));
            foreach (TaskContainer project in projects)
            {
                Release[] releases = Release.FindAll(Expression.Eq("TaskContainerID", project.ID), Expression.Eq("Revision", 0));
                foreach (Release r in releases)
                {
                    Task[] tasks = Task.FindAll(Expression.Eq("ReleaseID", r.ID));
                    int releaseTaskTotal = tasks.Length;
                    int tasksQAReady = 0;
                    decimal taskPercentage = 0;
                    foreach (Task t in tasks)
                    {
                        if (t.IsResolved)
                        {
                            tasksQAReady++;
                        }
                    }
                    if (releaseTaskTotal > 0)
                    {
                        taskPercentage = (Convert.ToDecimal(tasksQAReady) / Convert.ToDecimal(releaseTaskTotal)) * 100;
                    }//this.className='highlightRow';
                    releaseList.Append("<tr id=\"tr" + r.ID + "\" OnClick=\"getTasks(" + r.ID + ", 'tr" + r.ID + "', '"+ project.Label.Replace("'", "''") +"');\" OnMouseOver=\"checkRollOver(true, 'tr" + r.ID + "');this.style.cursor='pointer';\" OnMouseOut=\"checkRollOver(false, 'tr" + r.ID + "');\"><td width=10></td><td>" + r.Major + "." + r.Minor + "." + r.Build + "</td><td>" + project.Label + "</td><td align=right>" + Math.Round(taskPercentage) + "%</td><td width=10></td></tr>");
                }
            }
            
        }
    }
}
